import threading

# 线程的唯一标识：get_ident
from threading import get_ident

"""
storage
{
    1232:{'num':123},
    1213:{'num':123}
}
"""


class Local(object):
    def __init__(self):
        object.__setattr__(self, "storage", {})

    def __setattr__(self, key, value):
        # 获取当前线程id
        ident = threading.get_ident()
        if ident in self.storage:  # 存在该线程则直接赋值
            self.storage[ident][key] = value
        else:  # 不存在该线程则增加新的元素
            self.storage[ident] = {key: value}

    def __getattr__(self, item):
        # 获取当前线程id
        ident = threading.get_ident()
        # 根据当前ident获取值

        if ident not in self.storage:  # 不存在该线程则直接返回空
            return
        return self.storage[ident].get(item)  # 存在该线程则返回该值


local = Local()


def task(args):
    local.num = args
    print(local.num)


for i in range(5):
    t = threading.Thread(target=task, args=(i,))
    t.start()
